package com.iotechn.unimall.admin.api.area;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.AreaDO;
import com.iotechn.unimall.data.mapper.AreaMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class AdminAreaServiceImpl implements AdminAreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public Page<AreaDO> list(Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<AreaDO> wrapper = new EntityWrapper<AreaDO>();
        /**if (!StringUtils.isEmpty(name)) {
         wrapper.like("username", name);
         }**/
        wrapper.orderBy("id", false);
        Integer count = areaMapper.selectCount(wrapper);
        List<AreaDO> AreaDOS = areaMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(AreaDOS, page, limit, count);
    }

    public List<AreaDO> areaList() throws ServiceException {
        Wrapper<AreaDO> wrapper = new EntityWrapper<AreaDO>();
        /**if (!StringUtils.isEmpty(name)) {
         wrapper.like("username", name);
         }**/
        wrapper.orderBy("id", false);
        Integer count = areaMapper.selectCount(wrapper);
        List<AreaDO> areaDOS = areaMapper.selectList(wrapper);
        return areaDOS;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AreaDO create(AreaDO areaDO, Long adminId) throws ServiceException {
        Date now = new Date();
        areaDO.setGmtCreate(now);
        areaDO.setGmtUpdate(now);
        if (areaMapper.insert(areaDO) > 0) {
            areaDO.setId(areaDO.getId());
            return areaDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(AreaDO areaDO, Long adminId) throws ServiceException {
        Long id = areaDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        areaDO.setGmtUpdate(now);
        if (areaMapper.updateById(areaDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (areaMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public AreaDO detail(Long id, Long adminId) throws ServiceException {
        return areaMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        AreaDO areaDO = new AreaDO();
        areaDO.setId(id);
        areaDO.setStatus(status);
        areaDO.setGmtUpdate(new Date());
        if (areaMapper.updateById(areaDO) > 0) {
            return true;
        }
        return false;
    }

}